import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-set-name',
  templateUrl: './set-name.component.html',
  styleUrls: ['./set-name.component.css'],
})
export class SetNameComponent implements OnInit {
  visible = false;
  tableForm!: FormGroup;

  @Output() result = new EventEmitter();
  constructor(private fb: FormBuilder, private message: NzMessageService) {}

  ngOnInit(): void {
    this.tableForm = this.fb.group({ name: '' });
  }
  open(): void {
    this.tableForm.reset();
    this.visible = true;
  }

  close(): void {
    this.visible = false;
  }

  // 设置源数据组件目标表格
  submitForm(): void {
    if (
      this.tableForm.controls.name.value != '' &&
      this.tableForm.controls.name.value != '<未命名表格>'
    ) {
      if (/^\d+$/.test(this.tableForm.controls.name.value)) {
        this.message.error('表名不能为纯数字');
        return;
      }
      this.result.emit(this.tableForm.controls.name.value);
      this.close();
    } else {
      this.message.error('表格名不能为空');
    }
  }
}
